﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace SGUI
{
    public class SGImage : UIElement
    {
        Texture tex;
        public override void Init() 
        {
            base.Init();
            this.AddProperty("Src", "");
        }
       
        public override void OnEnter()
        {
            this.tex = (Texture)Resources.Load((string)this.GetProperty("Src").Value);
            this.SetProperty("Width",(double)this.tex.width,false);
            this.SetProperty("Height", (double)this.tex.height, false);
            this.GenSelfMesh();
            GenRender();
            UpdateToRender();
            base.OnEnter();
        }

        public override void GenSelfMesh()
        {
           
            SimpleFill();
        }
        void SimpleFill()
        {
            this.mGeom = new UIGeometry();
            this.mGeom.verts = new List<Vector3>();
            this.mGeom.cols = new List<Color>();
            this.mGeom.uvs = new List<Vector2>();
            this.color = SGTools.ColorFromHTML(this.PropertyValue<string>("Color"));
            //Vector
            Vector4 size = this.GetElementSize() ;
            float Scale = 1f;//(float)this.PropertyValue<double>("Scale");
            this.mGeom.verts.Add(new Vector3(size.x * Scale, size.y * Scale, 0)); //minx,miny
            this.mGeom.verts.Add(new Vector3(size.z * Scale, size.y * Scale, 0)); //maxx,miny
            this.mGeom.verts.Add(new Vector3(size.x * Scale, size.w * Scale, 0)); //minx,maxy
            this.mGeom.verts.Add(new Vector3(size.z * Scale, size.w * Scale, 0)); //maxx,maxy
            //Color
            this.mGeom.cols.Add(this.color);
            this.mGeom.cols.Add(this.color);
            this.mGeom.cols.Add(this.color);
            this.mGeom.cols.Add(this.color);
            //UV
            this.mGeom.uvs.Add(new Vector2(0,0));
            this.mGeom.uvs.Add(new Vector2(1, 0));
            this.mGeom.uvs.Add(new Vector2(0, 1));
            this.mGeom.uvs.Add(new Vector2(1, 1));
        }
        void GenRender()
        {
            MeshRenderer render = this.gameObject.AddComponent<MeshRenderer>();
            MeshFilter mf = this.gameObject.AddComponent<MeshFilter>();
            render.material = new Material(Shader.Find("SGUIShader/Texture"));
            render.material.mainTexture = tex;
            render.material.color = this.color;
        }
        public override void UpdateToRender()
        {
            MeshRenderer render = this.gameObject.GetComponent<MeshRenderer>();
            MeshFilter mf = this.gameObject.GetComponent<MeshFilter>();
            Mesh mesh = new Mesh();
            mesh.vertices = this.mGeom.verts.ToArray();
            mesh.colors = this.mGeom.cols.ToArray();
            mesh.uv = this.mGeom.uvs.ToArray();
            mesh.SetIndices(new int[] { 0, 1, 2, 1, 3, 2 }, MeshTopology.Triangles, 0);
            mf.sharedMesh = mesh;
            render.material.mainTexture = tex;
        }

    }
}
